//
//  demo01ViewController.m
//  demo30导航栏透明
//
//  Created by bing on 15/11/10.
//  Copyright © 2015年 bing. All rights reserved.
//

#import "demo01ViewController.h"
#import "demo02ViewController.h"

@interface demo01ViewController ()
@property (nonatomic, weak) UIView *v;
@end

@implementation demo01ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 获取导航栏的背景色
    for (UIView *sub in self.navigationController.navigationBar.subviews) {
        if ([sub isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            _v = sub;
            break;
        }
    }

    // 调整右侧返回文字位置
    self.navigationItem.title = @"demo 01";
    self.navigationController.navigationBar.translucent = YES;
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    UIImage *image = [self buttonImageFromColor:[UIColor redColor]];
    //    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    
    // 使导航栏切换渐变
    UIImage *image = [self buttonImageFromColor:[UIColor redColor]];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    _v.alpha = 0.0;
    [UIView animateWithDuration:0.3 animations:^{
        _v.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    demo02ViewController *vc = [demo02ViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

//通过颜色来生成一个纯色图片
- (UIImage *)buttonImageFromColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
@end
